﻿using B.S.DataBase.MyProject.Domain.基础设置;
using B.S.DataBase.MyProject.Domain.知识管理;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.KnowledgeManagement;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.KnowledgeManagement
{
    /// <summary>
    /// 修改知识管理-我的知识的的更改处理方法
    /// </summary>
    public class UdpKnowledgeCommandHandler : IRequestHandler<UdpKnowledgeCommand, APIResult<int>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;

        public UdpKnowledgeCommandHandler(SqlSugarContext sugar)
        {
            this.sugar = sugar;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(UdpKnowledgeCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                var model = await sugar.db.Queryable<KnowledgeManagement>().Where(x => x.KnowledgeID == request.KnowledgeID).FirstAsync();
                model.Theme = request.Theme;
                model.ApplicableDisease = request.ApplicableDisease;
                model.IsPublic = request.IsPublic;
                model.ThemeContent = request.ThemeContent;
                var r = await sugar.db.Updateable(model).ExecuteCommandAsync();
                if (r > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = r;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = r;
                }


            }
            catch (Exception ex)
            {

                // 记录异常信息
                Console.WriteLine($"修改知识管理数据时发生异常: {ex.Message}");
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = 0;
            }
            return await Task.FromResult(result);
        }
    }

}
